package com.woniu.controller;

import com.woniu.entity.*;
import com.woniu.service.AddressService;
import com.woniu.util.R;
import com.woniu.vo.AddressVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/address")
public class AddressController {
    @Resource
    private AddressService addressService;

    @GetMapping("/getAddress/{accountId}")
    public R getAddress(@PathVariable Integer accountId) {
        List<AddressVo> list = addressService.findAddressByUserId(accountId);
        return R.build(200, "查询地址成功", list);
    }

    @GetMapping("/findProvinces")
    public R findProvinces() throws Exception {
        List<Provinces> provincesByCode = addressService.findProvincesByCode();
        return R.build(200, "查询成功", provincesByCode);
    }

    @GetMapping("/findCities/{selectedProvince}")
    public R findCities(@PathVariable String selectedProvince) throws Exception {
        List<Cities> citiesByProvinceCode = addressService.findCitiesByProvinceCode(selectedProvince);
        return R.build(200,"查询成功", citiesByProvinceCode);
    }

    @GetMapping("/findAreas/{selectedCity}")
    public R findAreas(@PathVariable String selectedCity) throws Exception {
        List<Areas> areasBycityCode = addressService.findAreasByCityCode(selectedCity);
        return R.build(200,"查询成功", areasBycityCode);
    }

    @GetMapping("/findStreets/{selectedCountry}")
    public R findStreets(@PathVariable String selectedCountry) throws Exception {
        List<Streets> streetsByAreaCode = addressService.findStreetsByAreaCode(selectedCountry);
        return R.build(200,"查询成功", streetsByAreaCode);
    }

    @GetMapping("/findVillages/{selectedStreet}")
    public R findVillages(@PathVariable String selectedStreet) throws Exception {
        List<Villages> villagesByStreetCode = addressService.findVillagesByStreetCode(selectedStreet);
        return R.build(200,"查询成功", villagesByStreetCode);
    }

    /**
     * 增加用户收获地址 如果超过5条 则无法继续增加
     */
    @PostMapping("/addAddress")
    public R addAddress(@RequestBody UserAddress userAddress) throws Exception {
        Integer accountId = userAddress.getAccountId();
        int i = addressService.selectAddressCountByUserId(accountId);
        if (i >= 5) {
            return R.error("地址达到上限，无法添加");
        } else {
            int result = addressService.addAddressByUserId(userAddress);
            return R.build(200,"增加成功", result);
        }
    }

    @PutMapping("/saveEditAddress")
    public R saveEditAddress(@RequestBody UserAddress userAddress) throws Exception {
        boolean result = addressService.editAddress(userAddress);
        if (result) {
            return R.build(200,"修改成功", result);
        } else {
            return R.error("修改失败");
        }
    }

    @PostMapping("/deleteAddress/{addressId}")
    public R deleteAddress(@PathVariable Integer addressId) throws Exception {
        boolean result = addressService.deleteAddressByAddressId(addressId);
        if (result) {
            return R.build(200,"删除成功", result);
        } else {
            return R.error("删除失败");
        }
    }

}
